<template>
	<view class="vmall-my">
		<view class="header-top">
			<StatusBar v-show="isStatusBar" />
		</view>

		<view class="wrap">
			<view class="myheader">
				<view class="flex items-center mb-40" @click="toNextPage({ url: '/pages/user/info' })">
					<view class="avatar">
						<up-avatar size="48" class="image" :src="userInfo.header"></up-avatar>
					</view>
					<view class="ml-40">
						<view class="username mb-20">{{ userInfo.name }}</view>
						<view class="standing">{{ RoleValue(role) }}</view>
					</view>
					<view class="chevronright-content ml-40">
						<image class="chevronright" :src="IMG_URL2+'common/chevron-right.svg'"></image>
					</view>
				</view>
				<view class="flex items-center mb-12">
					<view class="flex1 flex items-center justify-center"
						@click="toNextPage({ url: `/pages/user/points?value=${integration}` })">
						<view class="subtit">积分</view>
						<view class="subvalue ml-40">{{ integration }}</view>
					</view>
					<view v-if="role&&role==2" class="flex1 flex items-center justify-center"
						@click="toNextPage({ url: `/pages/user/balance?value=${balance}` })">
						<view class="subtit">余额</view>
						<view class="subvalue ml-40">{{ balance }}</view>
						<view class="subfix ml-20">元</view>
					</view>
					<view v-else class="flex1 flex items-center justify-center">

					</view>
				</view>
			</view>
			<view class="warppadding">
				<view class="shopinfo-grid" @click="toNextPage1({ url: '/pagesshop/shop/shopmanage' })">
					<view class="">
						<view class="tit mb-20">{{ shopList[0].name }}</view>
						<view class="shopinfo-grid-text">经营时间：{{ shopList[0].workTime }}</view>
					</view>
					<view>
						<image class="image" :src="IMG_URL2+'mine/caret-right-small.svg'"></image>
					</view>
				</view>
				<view class="order-grid">
					<view class="grid-content">
						<up-grid :col="5" :border="false">
							<up-grid-item v-for="(item, index) in orderGridList" :index="index" :key="index"
								@click="onOrder(item.targetType)">

								<img class="grid-image" :src="item.image" />
								<text class="grid-text"> {{ item.name }}</text>
								<up-badge :absolute="true" :offset="[0,2]" max="99" :value="item.value"></up-badge>
							</up-grid-item>
						</up-grid>
					</view>
				</view>
				<view class="other-grid">
					<view class="order-grid-other">功能区</view>
					<view class="grid-content1">
						<up-grid :col="4" :border="false">
							<up-grid-item v-for="(item, index) in otherGridList" :index="index" :key="index"
								@click="toNextPage(item)">
								<img class="grid-image1" :src="item.image" />
								<text class="grid-text1"> {{ item.name }}</text>
							</up-grid-item>
						</up-grid>
					</view>
				</view>
			</view>

		</view>
		<BackTop :backTop="backTop" />
		<SalesButton></SalesButton>
	</view>
</template>


<script setup>
import { IMG_URL2 } from "@/config";
import StatusBar from "@/components/status-bar.vue";
import BackTop from "@/components/back-top.vue";
import SalesButton from "@/components/salesbutton.vue";
import { studyApi,shopApi } from "@/api";
const isStatusBar = ref(false);
const navbarColor = ref("rgba(0, 0, 0, 0)");
const isNavbarShow = ref(false);
let balance = ref(0);//余额
let integration = ref(0);//积分
let role = ref(0)//角色
import { getUserInfo } from "@/service";
let userInfo1Storge = getUserInfo();
const badgetype = ref('warning');
const userInfo = ref({
	header: '',
	name: "Baymax",
});
let shopList = ref([{ name: 'XXXXX店铺名称', workTime: '9:00-22:00' }])
const isAccountInfo = ref(false);
const orderGridList = ref([
  {
    id: 0,
    name: "待审核",
	value:'',
    image:IMG_URL2+'mine/hourglass.svg',
    url: "",
    targetType:1,
  },
  {
    id: 1,
    name: "待发货",
	value:'',
    image:IMG_URL2+'mine/box.svg',
    url: "",
    targetType:2,
  },
  {
    id: 2,
    name: "待收货",
	value:'',
    image:IMG_URL2+'mine/truck.svg',
    url: "",
    targetType:3,
  },
  {
    id: 3,
    name: "已完成",
    image:IMG_URL2+'mine/check-circle.svg',
    url: "",
    targetType:4,
  },
  {
    id: 4,
    name: "全部订单",
    image:IMG_URL2+'mine/view-module.svg',
    url: "",
    targetType:0
  }
]);

const otherGridList = ref([
  {
    name: "收藏商品",
    targetType:'',
    url: "/pages/user/collection",
    image:IMG_URL2+'mine/star-filled.svg',
	isShow: true,
  },
  {
    name: "店员管理",
    url: "/pages/user/shopassistant",
    targetType:'',
    image:IMG_URL2+'mine/user-circle.svg',
	isShow: true,
  },
  {
    name: "联系客服",
    url: "/pages/user/customerservice",
    targetType:'',
    image:IMG_URL2+'mine/service.svg',
	isShow: true,
  },
  {
    name: "收货地址",
    url: "/pages/user/address/index",
    targetType:'',
    image:IMG_URL2+'mine/location.svg',
	isShow: true,
  },
  {
    name: "会员管理",
    url: "/pagesshop/member/index",
    targetType:'',
    image:IMG_URL2+'mine/user.svg',
	isShow: true,
  },
  {
    name: "收款资料",
    url: "/pagesshop/collectionofpayment/index",
    targetType:'',
    image:IMG_URL2+'mine/qrcode.svg',
	isShow: true,
  },
  {
    name: "数据统计",
    url: "/pagesshop/businessdata/index",
    targetType:'',
    image:IMG_URL2+'mine/chart.svg',
	isShow: true,
  },
  {
    name: "设置",
    url: "/pages/user/settings",
    targetType:'',
    image:IMG_URL2+'mine/setting.svg',
	isShow: true,
  }
]);
// 返回顶部
const backTop = ref(0);

function RoleValue(val) {
	if (val==0) {
		return '店长'
	}else if (val == 1){
		return '店员'
	}else if (val == 2) {
		return '专营商'
	}
}

onLoad(() => {
	// 初始化小程序和APP状态栏的颜色
	//   uni.setNavigationBarColor({
	// 	frontColor: "#ffffff",
	// 	backgroundColor: "#fcc53a"
	//   });

	// uni.getStorage({
	// 	key: "accountInfo",
	// 	success: (res) => {
	// 		isAccountInfo.value = res.data ? true : false;
	// 	}
	// });

});

// 监听自定义事件并进行页面刷新操作
onShow(() => {
	getPersonInfo()
	getOrderCount()
	uni.$on("refresh", (data) => {
		if (data.refresh) {
			// 刷新操作
			isAccountInfo.value = false;
		}
	});
});

onUnload(() => {
	uni.$off("refresh"); // 需要手动解绑自定义事件

});

const getPersonInfo = () => {
	studyApi.FRONTSTAFFGETPERSONINFO().then(res => {
		balance.value = res.result.balance?res.result.balance:0
		integration.value = res.result.integration?res.result.integration:0
		userInfo.value.name = res.result.name
		userInfo.value.header = res.result.header
		role.value = res.result.role
		shopList.value[0].name = res.result.shopName
		shopList.value[0].workTime = res.result.workTime
		if(res.result.role==1){
			// 店员角色 不显示店员管理
			otherGridList.value = otherGridList.value.filter(item => item.name !== "店员管理")
		}
		// 专营商显示收款资料
		if(res.result.role!=2){
			otherGridList.value = otherGridList.value.filter(item => item.name !== "收款资料")
			otherGridList.value = otherGridList.value.filter(item => item.name !== "收货地址")
			if (!userInfo1Storge.isOpaDataCountShowAuth) {
				otherGridList.value = otherGridList.value.filter(item => item.name !== "数据统计")
			}
		}

	})
}


const getOrderCount = () => {
	shopApi.ORDERGETORDERCOUNT().then(res => {
		if(res.code==200){
			console.log(res.result)
			orderGridList.value[0].value = res.result.waitAudit //	待审核
			orderGridList.value[2].value = res.result.waitReceive	//待收货
			orderGridList.value[1].value = res.result.waitSend //待发货

		}
	})
}





// 注册&&登录
const onLogin = () => {
	uni.getProvider({
		service: "oauth",
		success: function (res) {
			console.log(res.provider);
		}
	});
	uni.login({
		success: (res) => {
			// res.code 即为获取到的 code
			console.log("登录", res);
			uni.setStorage({
				key: "accountInfo",
				data: true,
				success: () => {
					isAccountInfo.value = true;
				}
			});
		},
		fail(e) {
			console.log("fail", e);
		},
		complete(e) {
			console.log("complete", e);
		}
	});
};

// 监听页面滚动(返回顶部)
onPageScroll((e) => {
	// 动态修改小程序和APP状态栏的颜色
	uni.setNavigationBarColor({
		// frontColor: e.scrollTop > 0 ? "#000000" : "#ffffff",
		// backgroundColor: e.scrollTop > 0 ? "#ffffff" : "#fcc53a"
	});
	//   isStatusBar.value = e.scrollTop > 0;
	//   navbarColor.value = e.scrollTop == 0 ? "rgba(255,255,255,0)" : "rgba(255,255,255,1)";
	//   isNavbarShow.value = e.scrollTop > 0;
	backTop.value = e.scrollTop;
});

// 触底加载
onReachBottom(() => {
	console.log("触底加载");
});
// 下拉刷新
onPullDownRefresh(() => {
	console.log("下拉刷新");
	// 正常情况下接口返回应该很会很快。故意延迟调用，让用户有在刷新的体验感
	getPersonInfo()
	setTimeout(() => {
		uni.stopPullDownRefresh();
	}, 300);
});

// 我的订单
const onOrder = (type) => {
	uni.navigateTo({ url: "/pages/order/index?type=" + type });
};

// 去往其他模块
const toNextPage = (item) => {
	if (item.url) {
		uni.navigateTo({
			url: item.url
		});
	}
};

// 专营商、店长可以查看店铺信息
const toNextPage1 = (item) => {
	if(role.value!=1){
	if (item.url) {
		uni.navigateTo({
			url: item.url
		});
	}
}
};
</script>

<style>
page {
	background-color: #f4f4f4;
}
</style>
<style lang="scss" scoped>
.vmall-my {
	.search-sticky {

		/* #ifdef APP-PLUS */
		:deep .u-navbar--fixed {
			// top: var(--status-bar-height);
		}

		/* #endif */
	}

	.header {
		// background-image: $andry-bg-image;
		// background-size: cover;
		background: linear-gradient(to top, #ededed, #ffdd8f, #fcc53a);
		// background: #fcc53a;
		padding: 50rpx 26rpx 100rpx 26rpx;
		/* #ifdef APP-PLUS */
		padding: calc(var(--status-bar-height) + 50rpx) 26rpx 100rpx 26rpx;
		/* #endif */
		display: flex;
		box-sizing: border-box;


		.user-login {
			display: flex;
		}
	}

	.myheader {
		padding: 0 24rpx 0rpx;
		box-sizing: border-box;
		background: linear-gradient(180deg, #FFFFFF 0%, #F5F5F5 100%);

		.avatar {
			width: 96rpx;
			height: 96rpx;
			border-radius: 50%;

			.image {
				width: 100%;
				height: 100%;
			}
		}

		.username {
			font-size: 36rpx;
			font-weight: 600;
			text-align: center;
			line-height: 40rpx;
		}

		.standing {
			font-size: 10px;
			text-align: center;
			color: #666666;
			line-height: 16rpx;
			background: rgba(231, 231, 231, 1);
			border-radius: 20rpx;
			padding: 4rpx 16rpx;
			box-sizing: border-box;
			display: inline-block;
		}

		.chevronright-content {
			width: 32rpx;
			height: 32rpx;

			.chevronright {
				width: 100%;
				height: 100%;
			}
		}

		.subtit {
			font-family: PingFang SC;
			font-size: 14px;
			font-weight: 400;
			line-height: 22px;
			text-align: center;
			color: rgba(0, 0, 0, 0.6);

		}

		.subvalue {
			font-family: PingFang SC;
			font-size: 20px;
			font-weight: 600;
			line-height: 28px;
			text-align: center;

		}

		.subfix {
			//styleName: Body/ExtraSmall;
			font-family: PingFang SC;
			font-size: 10px;
			font-weight: 400;
			line-height: 16px;
			text-align: center;

		}
	}

	.wrap {
		.warppadding {
			padding: 20rpx 24rpx;
			box-sizing: border-box;
		}

		.shopinfo-grid {
			padding: 30rpx 60rpx 30rpx 60rpx;
			box-sizing: border-box;
			border-radius: 4px;
			background-color: rgba(245, 237, 224, 1);
			display: flex;
			align-items: center;
			justify-content: space-between;

			.tit {
				font-family: PingFang SC;
				font-size: 16px;
				font-weight: 600;
				line-height: 24px;
				color: rgba(77, 41, 29, 1);
			}

			.shopinfo-grid-text {
				font-family: PingFang SC;
				font-size: 14px;
				font-weight: 400;
				line-height: 22px;
				color: rgba(150, 124, 97, 1);
			}

			.image {
				width: 16px;
				height: 16px;
			}

		}

		.order-grid {
			margin: 16rpx 10rpx;
			border-radius: 8rpx;
			background: #ffffff;
			color: #666666;
		}

		.grid-image {
			width: 40rpx;
			height: 40rpx;
			margin-bottom: 10rpx;
		}

		.grid-text {
			font-size: 24rpx;
			padding-bottom: 28rpx;
		}

		.grid-content {
			padding: 28rpx 12rpx 0rpx 12rpx;
			box-sizing: border-box;
			.badge {
				z-index: 100;
			}
		}

		.grid-content1 {
			padding: 28rpx 0rpx 0rpx 0rpx;
			box-sizing: border-box;
			.badge {
				z-index: 100;
			}

			.grid-image1 {
				width: 44rpx;
				height: 44rpx;
				margin-bottom: 8rpx;
			}

			.grid-text1 {
				font-size: 24rpx;
				padding-bottom: 28rpx;
			}

		}

		.other-grid {
			margin: 16rpx 10rpx;
			padding: 24rpx 28rpx;
			box-sizing: border-box;
			border-radius: 8rpx;
			background: #ffffff;
			color: #666666;

			.order-grid-other {
				font-size: 24rpx;
				font-weight: 600;
				line-height: 20px;
				text-underline-position: from-font;
				text-decoration-skip-ink: none;

				color: rgba(0, 0, 0, 0.9);
			}

			.scroll-list__goods-item__image {
				display: block;
				margin: 0 auto;
			}
		}

		.scroll-list__goods-item {

			margin-right: 20rpx;
			text-align: center;

			.scroll-list__goods-item__image {
				width: 100rpx;
				height: 100rpx;
				border-radius: 8rpx;
			}

			.scroll-list__goods-item__text {
				display: inline-block;
				text-align: center;
				font-size: 24rpx;
				margin-top: 10rpx;
				width: 120rpx;
			}
		}

		.u-scroll-list {
			padding-bottom: 0rpx;
		}
	}
}

.setting {
	display: flex;
	align-items: flex-start;
	margin-top: 6rpx;
}

.chat {
	position: relative;
	margin-left: 16rpx;
	margin-right: 8rpx;
}

:deep(.u-status-bar) {
	display: none !important;
}
</style>
